home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / otext.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  5.9 KB  |  232 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * otext.c : openGL (motif) demo to draw bitmapped fonts using display lists
  20.  *
  21.  * Author : Yusuf Attarwala
  22.  *          SGI - Applications
  23.  *
  24.  * Date   : Feb 93
  25.  *
  26.  *---------------------------------------------------------------------------*/
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #include <Xm/Xm.h> 
  31. #include <Xm/Frame.h>               /* for frame widgets */
  32.  
  33. #include <GL/gl.h>                  /* gl includes */
  34. #include <GL/glu.h>                 /* utility library includes */
  35. #include <GL/GLwMDrawA.h>           /* include for the drawing area widget */
  36.  
  37. /* function declarations */
  38.  
  39. int 
  40.         main(int,char **);
  41. static void 
  42.             createToplevel(void),
  43.             drawScene(void),
  44.             setMatrix(void),
  45.             exposeCB(Widget, XtPointer, XtPointer),
  46.             resizeCB(Widget, XtPointer, XtPointer),
  47.             initCB(Widget, XtPointer, XtPointer);
  48.  
  49. void        makeFontSet(void),
  50.         myCharStr(char *);
  51.  
  52.  
  53. /* global variables */
  54.             
  55. Display       *display;             /* current display */
  56. XtAppContext  appContext;           /* X application context */
  57. GLuint        base;
  58.  
  59. Widget       toplevel,       /* toplevel shell */
  60.              glw;            /* current widget */
  61.              
  62. GLXContext   glxc;           /* current glx context */
  63.  
  64. char outputText[125];
  65. static  GLfloat yellow[3] = {1.0,1.0,0.0};
  66.  
  67. Arg args[20];
  68. int acnt;
  69.  
  70. int 
  71. main(int argc, char** argv)
  72. {
  73.     int i;
  74.     char oneWord[60];
  75.  
  76.     /* parse arguments, etc */
  77.     if (argc > 1) {
  78.     outputText[0] = '\0';
  79.     for (i=1;i<argc;i++) {
  80.         sprintf(oneWord,"%s ",argv[i]);
  81.         strcat(outputText,oneWord);
  82.     }
  83.     }
  84.     else {
  85.     fprintf(stderr,"Usage : %s string1 [string2...]\n",argv[0]);
  86.     strcpy(outputText,"HELLO WORLD");
  87.     }
  88.  
  89.     XtToolkitInitialize();
  90.     appContext = XtCreateApplicationContext();
  91.     display    = XtOpenDisplay(appContext, NULL, "Otext","otext",NULL,0,
  92.                               &argc,argv);
  93.     if (!display) {
  94.         printf("%s : Unable to open display\n",argv[0]);
  95.         exit(0);
  96.     }
  97.     createToplevel();             /* create and realize widget hierarchy */
  98.     XtAppMainLoop(appContext);    /* loop for ever */
  99. }
  100.  
  101.  
  102.  
  103. void
  104. createToplevel(void)
  105. {
  106.     Widget frame;
  107.  
  108.     acnt = 0;
  109.     XtSetArg(args[acnt],XmNminHeight, 200);acnt++;
  110.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  111.     toplevel  = XtAppCreateShell("openGL text","xtext",
  112.                                   applicationShellWidgetClass,
  113.                                   display,args,acnt);
  114.  
  115.     /* create a frame to hold the glx drawing area */
  116.     frame   = XtVaCreateManagedWidget("frame", 
  117.                   xmFrameWidgetClass, toplevel, 
  118.                   NULL);
  119.  
  120.     /* create and manage a single buffer widget, rgb mode */
  121.     acnt = 0;
  122.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  123.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  124.     XtManageChild(glw);
  125.  
  126.     /* register callbacks */
  127.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  128.  
  129.     /* realize widget hierarchy */
  130.     XtRealizeWidget(toplevel);
  131.  
  132.     /* generate display lists for text bitmaps */
  133.     makeFontSet();
  134.  
  135.     /* additional callbacks */
  136.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  137.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  138.  
  139. }
  140.  
  141. void
  142. drawScene(void)
  143. {
  144.     glClearColor(0.0, 0.0, 0.0, 0.0);
  145.     glClear(GL_COLOR_BUFFER_BIT);
  146.     glColor3fv(yellow);
  147.     glRasterPos2i(10,10);
  148.     myCharStr(outputText);
  149.     glFlush();
  150. }
  151.  
  152. static void
  153. setMatrix(void)
  154. {
  155.     glMatrixMode(GL_PROJECTION);
  156.     glLoadIdentity();
  157.     glOrtho(0.0,100.0,0.0,100.0,-1.0,1.0); /* a simple ortho */
  158.     glMatrixMode(GL_MODELVIEW);
  159.     glLoadIdentity();                      /* default view */
  160. }
  161.  
  162. static void 
  163. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  164. {
  165.     GLwDrawingAreaCallbackStruct *call_data;
  166.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  167.  
  168.     GLwDrawingAreaMakeCurrent(w, glxc);
  169.     glViewport(0, 0, call_data->width, call_data->height);
  170.     setMatrix();
  171.     drawScene();
  172. }
  173.  
  174. static void 
  175. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  176. {
  177.     GLwDrawingAreaCallbackStruct *call_data;
  178.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  179.  
  180.     GLwDrawingAreaMakeCurrent(w, glxc);
  181.     drawScene();
  182. }
  183.  
  184. static void
  185. initCB(Widget w, XtPointer client_data, XtPointer call)
  186. {
  187.     XVisualInfo *vi;
  188.  
  189.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  190.     XtGetValues(w, args, 1);
  191.  
  192.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  193. }
  194.  
  195. void
  196. makeFontSet()
  197. {
  198.     XFontStruct *xfont;
  199.     unsigned int firstChar, lastChar;
  200.  
  201.     GLwDrawingAreaMakeCurrent(glw, glxc);
  202.  
  203.     xfont = XLoadQueryFont(display,"9x15");
  204.     /* replace 9x15 with your favorite font set */
  205.  
  206.     if (!xfont) {
  207.     fprintf(stderr,"FONT NOT FOUND \n");
  208.     exit(0);
  209.     }
  210.  
  211.     firstChar = xfont->min_char_or_byte2;
  212.     lastChar  = xfont->max_char_or_byte2;
  213.  
  214.     if (base = glGenLists(lastChar-firstChar+1)) {
  215.     glXUseXFont(xfont->fid,firstChar,lastChar-firstChar+1,base+firstChar);
  216.     }
  217.     else {
  218.     fprintf(stderr,"OUT OF DISPLAY LISTS\n");
  219.     exit(0);
  220.     }
  221. }
  222.  
  223. void
  224. myCharStr(char *s)
  225. {
  226.     glPushAttrib(GL_LIST_BIT);
  227.     glListBase(base);
  228.     glCallLists(strlen(s),GL_UNSIGNED_BYTE,(unsigned char *)s);
  229.     glPopAttrib();
  230. }
  231.  
  232.